import {
	ref
} from "vue";

import {
	onLoad
} from "@dcloudio/uni-app"

import {
	useDishesStore
} from "../../stores/useDishesStore.js";

import {
	useMenuApis
} from "../../request/menu.api.js"

import {
	useDishesApis
} from "../../request/dishes.api.js";

import {
	useWishlistApis
} from "../../request/wishlist.api.js";

export const useController = () => {

	const dishesStore = useDishesStore();
	const menuApis = useMenuApis();
	const dishesApis = useDishesApis();
	const wishlistApis = useWishlistApis();

	// 菜单日期
	const menuDate = ref("");
	// 分类索引
	const sideBarIndex = ref(0);
	// 分类菜品列表
	const dishesList = ref([]);
	// 购物车列表
	const dishesListInCart = ref([]);

	function isSelected(dishesId) {
		return dishesListInCart.value.some(e => e.id === dishesId);
	}

	// 获取今日菜单中的菜品
	async function getMenu() {
		const data = await menuApis.getMenuByDate(menuDate.value);
		dishesListInCart.value = data.dishes;
	}

	// 添加菜品到菜单
	function addDishesToMenu(dishes) {
		const duplicate = dishesListInCart.value.some(e => e.id === dishes.id);
		if (duplicate) return;
		dishesListInCart.value.push(dishes);
	}

	// 从菜单中移除菜品
	function removeDishesFromCartList(dishesId) {
		console.log('从菜单中移除菜品', dishesId);
		dishesListInCart.value = dishesListInCart.value.filter(e => e.id !== dishesId);
	}

	// 设置评分榜菜品列表
	function setRatingsRanking() {
		console.log("sideBarIndex", sideBarIndex.value);
		// dishesList.value = dishesStore.ratingsRanking;
		// 
	}

	// 设置想吃榜菜品列表
	function setWishlistRanking() {
		console.log("sideBarIndex", sideBarIndex.value);
		// dishesList.value = dishesStore.wishlistRanking;

	}

	// 设置分类菜品列表
	async function setDishesListByCategory() {
		console.log("sideBarIndex", sideBarIndex.value);
		const idx = sideBarIndex.value;
		let list = []

		if (idx === 0) {
			// 评分榜
			list = await dishesApis.getDishesList();
			list = list.sort((a, b) => b.avgRating - a.avgRating);
		} else if (idx === -1) {
			// 想吃榜
			list = await wishlistApis.getWishlist();
		} else {
			list = await dishesApis.getDishesListByCategoryId(sideBarIndex.value);
		}

		dishesList.value = list;
	}

	// 获取所有菜品，保存到dishesStore，并设置默认评分榜的菜品
	async function initDishesList() {
		setDishesListByCategory();
		// const list = await dishesApis.getDishesList();
		// dishesStore.dishesList = list;
		// dishesList.value = dishesStore.ratingsRanking;
	}


	// 删除菜品
	async function deleteDishes(dishesId) {
		await dishesApis.deleteDishes(dishesId);
	}

	// 设置修改今日菜单
	function updateMenu() {
		const date = menuDate.value;
		const dishesIdList = dishesListInCart.value.map(e => e.id);
		return menuApis.updateMenu({
			date,
			dishesIdList,
		});
	}

	onLoad(() => {
		initDishesList()
	});


	return {
		menuDate,
		sideBarIndex,
		dishesList,
		dishesListInCart,

		isSelected,

		getMenu,
		addDishesToMenu,
		removeDishesFromCartList,
		updateMenu,
		setDishesListByCategory,
		deleteDishes,
		updateMenu,
	}
}